package com.and9.mchen.mcmusic.dao.impl;

import java.sql.Timestamp;
import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IBaseEntityDao;
import com.and9.mchen.mcmusic.dao.IImageUrlDao;
import com.and9.mchen.mcmusic.entity.ImageUrl;
import com.and9.mchen.mcmusic.exception.DBException;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.util.EntityHelper;
import com.and9.mchen.mcmusic.util.GEntityHelper;
import com.and9.mchen.mcmusic.util.QueryHelper;

public class ImageUrlDaoImpl implements IImageUrlDao{

	private final static Log log = LogFactory.getLog(ImageUrlDaoImpl.class);

	public static String TABLEMANE="image_url_table";
	
	public int createOrUpdateImageUrl(ImageUrl imageUrl, String... propertyNames) throws DaoException {
		
		if(GEntityHelper.isEmpty(imageUrl)){
			return -1;
		}
		
		String sqlStr=null;
		Object[] propertyValues=null;
		
		if(imageUrl.getId() == null || imageUrl.getId() <= 0){
			
			if(GEntityHelper.isEmpty(imageUrl.getUniqueId())){
				imageUrl.setUniqueId(EntityHelper.createUniqueId());
			}
			
			imageUrl.setCreateTime(new Timestamp(System.currentTimeMillis()));
			Object[] parentParams=QueryHelper.getFieldsVal(imageUrl.getParent(), IBaseEntityDao.BASEPROPERTYNAMES);
			Object[] imageUrlParams=QueryHelper.getFieldsVal(imageUrl, IImageUrlDao.BASEPROPERTYNAMES);
			propertyValues=EntityHelper.groupArrays(parentParams, imageUrlParams);

			propertyNames=EntityHelper.groupArrays(IBaseEntityDao.BASEPROPERTYNAMES, IImageUrlDao.BASEPROPERTYNAMES);
			sqlStr=QueryHelper.getInsertSQLByProperty(ImageUrlDaoImpl.TABLEMANE, propertyNames);
			
		}else{
			
			if(imageUrl.getId().equals(1)){
				return 0;
			}
			
			if(GEntityHelper.isEmpty(propertyNames) || 
					EntityHelper.containBaseEntityPropertyName(propertyNames) == true){
				propertyNames=IImageUrlDao.BASEPROPERTYNAMES;
			}
			propertyValues=QueryHelper.getFieldsVal(imageUrl, propertyNames);
			sqlStr=QueryHelper.getUpdateSQL(ImageUrlDaoImpl.TABLEMANE, imageUrl,"id="+imageUrl.getId(), propertyNames);
		}
		
		try {
			
			return QueryHelper.update(sqlStr, propertyValues);
		} catch (DBException e) {
			log.error("[function: createOrUpdateImageUrl]", e);
			throw new DaoException("create or update image url faliure");
		}
	}

	public int deleteImageUrlByImageUrlId(long id) throws DaoException{
		
		if(id <= 0){
			return -1;
		}
		
		if(id == 0){
			return 0;
		}
		
		String sqlStr=QueryHelper.getDeleteSQL(ImageUrlDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.update(sqlStr, id);
		} catch (DBException e) {
			log.error("[function: deleteImageUrlByImageUrlId]", e);
			throw new DaoException("delete image url faliure");
		}
		
	}

	public ImageUrl getImageUrlByAlbumId(long id) throws DaoException {
	
		if(id <= 0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} " +
				"WHERE {0}.id={1}.image_url_id AND {1}.id=?", ImageUrlDaoImpl.TABLEMANE, 
					AlbumDaoImpl.TABLEMANE);
		
		try {
			return QueryHelper.read(ImageUrl.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getImageUrlByAlbumId]", e);
			throw new DaoException("get image url by album id faliure");
		}

	}

	public ImageUrl getImageUrlByImageUrlId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(ImageUrlDaoImpl.TABLEMANE, "id=?");

		try {
			return QueryHelper.read(ImageUrl.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getImageUrlByImageUrlId]", e);
			throw new DaoException("get image url by image url id faliure");
		}
		
		
	}

	public ImageUrl getImageUrlByImageUrlUnqiueId(String uniqueId)
			throws DaoException {
		if(GEntityHelper.isEmpty(uniqueId)){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(ImageUrlDaoImpl.TABLEMANE, "uniqueId=?");
		
		try {
			return QueryHelper.read(ImageUrl.class, sqlStr, uniqueId);
		} catch (DBException e) {
			log.error("[function: getImageUrlByImageUrlUnqiueId]", e);
			throw new DaoException("get image url by image url unqiueId failure");
		}
	}

	public ImageUrl getImageUrlBySingerId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} " +
				"WHERE {0}.id={1}.image_url_id AND {1}.id=?", ImageUrlDaoImpl.TABLEMANE, 
					SingerDaoImpl.TABLEMANE);
		
		try {
			return QueryHelper.read(ImageUrl.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getImageUrlBySingerId]", e);
			throw new DaoException("get image url by singer id faliure");
		}
	}

}
